package org.lzy.shop.view

import android.content.Context
import android.os.CountDownTimer
import android.util.AttributeSet
import android.view.View
import androidx.appcompat.widget.AppCompatTextView

/**
 * 倒计时按钮
 *
 */
class CountDownButton : AppCompatTextView, View.OnClickListener {
    interface CountDownButtonOperationCallBack {
        fun onFinish()

        fun onTick(millisUntilFinished: Long, countDownButton: CountDownButton?)

        fun onClickFromUser(
            isRunning: Boolean,
            millisUntilFinished: Long,
            countDownButton: CountDownButton?
        )
    }


    /**
     * 操作回调接口
     */
    var mCountDownButtonOperationCallBack: CountDownButtonOperationCallBack? = null
    var isRunning: Boolean = false

    /**
     * 倒计时总时间
     */
    var mMillisInFuture: Long = 60100

    /**
     * 间隔
     */
    var mCountDownInterval: Long = 1000


    var mCurrentmillis: Long = 0
    var mCountDownTimer: CountDownTimer? = null


    constructor(context: Context) : super(context) {
        setOnClickListener(this)
    }

    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
        setOnClickListener(this)
    }

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    ) {
        setOnClickListener(this)
    }

    fun setOnOperationCallBack(countDownButtonOperationCallBack: CountDownButtonOperationCallBack?) {
        this.mCountDownButtonOperationCallBack = countDownButtonOperationCallBack
    }

    fun start() {
        if (mCountDownTimer == null) {
            createCountTimer()
        }
        mCountDownTimer!!.start()
    }

    fun finish() {
        if (mCountDownTimer != null) {
            mCountDownTimer!!.onFinish()
        }
    }

    fun cancel() {
        if (mCountDownTimer != null) {
            mCountDownTimer!!.cancel()
        }
    }

    fun destroy() {
        if (mCountDownTimer != null) {
            mCountDownTimer!!.cancel()
        }
        mCountDownTimer = null
        mCountDownButtonOperationCallBack = null
    }

    fun getmCurrentmillis(): Long {
        return mCurrentmillis
    }

    /**
     * 倒计时时间间隔(毫秒)
     *
     */
    fun countDownInterval(countDownInterval: Long): CountDownButton {
        this.mCountDownInterval = countDownInterval + 100
        return this
    }

    /**
     * 倒计时总时间(毫秒)
     */
    fun millisInFuture(millisInFuture: Long): CountDownButton {
        this.mMillisInFuture = millisInFuture
        return this
    }

    fun createCountTimer() {
        if (mCountDownTimer != null) {
            mCountDownTimer!!.cancel()
            mCountDownTimer = null
        }
        mCountDownTimer = object : CountDownTimer(mMillisInFuture, mCountDownInterval) {
            override fun onTick(millisUntilFinished: Long) {
                isRunning = true
                this@CountDownButton.mCurrentmillis = millisUntilFinished
                if (mCountDownButtonOperationCallBack != null) {
                    mCountDownButtonOperationCallBack!!.onTick(mCurrentmillis, this@CountDownButton)
                }
            }

            override fun onFinish() {
                this@CountDownButton.mMillisInFuture = 0
                isRunning = false
                if (mCountDownButtonOperationCallBack != null) {
                    mCountDownButtonOperationCallBack!!.onFinish()
                }
            }
        }
    }


    override fun onClick(v: View?) {
        if (mCountDownButtonOperationCallBack != null) {
            mCountDownButtonOperationCallBack!!.onClickFromUser(
                isRunning,
                mCurrentmillis,
                this@CountDownButton
            )
        }
    }

    override fun onDetachedFromWindow() {
        super.onDetachedFromWindow()
        // 当视图从窗口分离时自动清理资源，防止内存泄漏
        destroy()
    }
}
